package com.java46.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.java46.entity.Candidate;
import com.java46.entity.Positi;
import com.java46.service.RecruitmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName RecruitmentController
 * @Description 招聘界面
 * @Author Tigger Wu
 * @Date 2018/8/1614:04
 * @Version 1.0
 **/

@Controller
@RequestMapping("/Recruitment")
public class RecruitmentController {
    @Autowired
    private RecruitmentService recruitmentService;

    /*
     * 查询全部目前暂缺的职位信息
     *
     * @author Tigger Wu
     * @date 2018/8/16 14:11
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/info",produces = "application/json;charset=utf-8")
    public String queryList() throws JsonProcessingException {
        List<Positi> clist = recruitmentService.querylist();
        // 2. 创建 ObjectMapper 对象
        ObjectMapper mapper= new ObjectMapper();
        //创建json字符串接收数据
        String jsonStr = mapper.writeValueAsString(clist);
        //返回json
        return jsonStr;
    }
/*
 * 添加预约人员信息
 *
 * @author Tigger Wu
 * @date 2018/8/16 17:06
 * @param
 * @return
 */
    @RequestMapping(value = "/add",produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public  String  addRec(Candidate candidate ,HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
       //创建列表
        List<String> vlist = new ArrayList<String>();
        try {
            //添加招聘信息
            candidate.setCanstate("0");
            recruitmentService.addRec(candidate);
            vlist.add("操作成功！");
        }catch (Exception e){
            vlist.add("数据异常，请稍后再尝试！");
            throw new RuntimeException("错误异常，请稍后再试！");
        }
        //转为json字符串
        ObjectMapper mapper= new ObjectMapper();
        String jsonStr =  mapper.writeValueAsString(vlist);
        return jsonStr;

    }
}
